package com.lmm.http.client;

import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.routing.HttpRoutePlanner;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HttpContext;

/**
 * httpclient4.3版本请求工具类
 * @author leiming
 *
 */
public class HttpClientUtils2 {
	//字符串返回数据
	public static final BasicResponseHandler basicResponseHandler = new BasicResponseHandler();
	

	//代理转发
	private static HttpRoutePlanner routePlanner = new HttpRoutePlanner() {

	    public HttpRoute determineRoute(
	            HttpHost target,
	            HttpRequest request,
	            HttpContext context) throws HttpException {
	    	System.out.println(target.getHostName());
	        return new HttpRoute(target, null,  new HttpHost("127.0.0.1", 7878),
	                "https".equalsIgnoreCase(target.getSchemeName()));
	    }

	};

   private static HttpClientBuilder httpClientBuilder = HttpClients.custom();
   private static BasicCookieStore cookieStore = new BasicCookieStore();
   private static CloseableHttpClient httpclient = httpClientBuilder
             .setRoutePlanner(routePlanner)
		   	.setDefaultCookieStore(cookieStore)
            .build();
   //浏览器User-Agent
   public static final String userAgent= "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.93 Safari/537.36";
   
   public static CloseableHttpClient getHttpClient(){
	   return  httpclient;
   }
}